﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

#endregion

#region COPYRIGHT

/*
    Copyright (c) 2007, 2008, 2009, 2010 
        Roland Rosenkranz (Glatzemann@email.de)
*/

#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Input.

    starLiGHT.Input is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Input is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Input.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Input are available on request.
*/

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1652                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-11-16 07:41:22 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.Input
{
#if WINDOWS || XBOX
    public class GamepadDevice : IControlDevice
    {
        GamePadState lastState;
        GamePadState actState;
        PlayerIndex playerIndex;

        bool wasEverConnected;

        ButtonControl buttonA;
        ButtonControl buttonB;
        ButtonControl buttonBack;
        ButtonControl buttonBigButton;
        ButtonControl buttonDPadDown;
        ButtonControl buttonDPadLeft;
        ButtonControl buttonDPadUp;
        ButtonControl buttonDPadRight;
        ButtonControl buttonLeftShoulder;
        ButtonControl buttonRightShoulder;
        ButtonControl buttonLeftStick;
        ButtonControl buttonRightStick;
        ButtonControl buttonStart;
        ButtonControl buttonX;
        ButtonControl buttonY;

        AxisControl leftStick;
        AxisControl rightStick;
        AnalogSingleAxisControl leftTrigger;
        AnalogSingleAxisControl rightTrigger;

        private GamepadDevice()
        {
            buttonA = new ButtonControl();
            buttonB = new ButtonControl();
            buttonBack = new ButtonControl();
            buttonBigButton = new ButtonControl();
            buttonDPadDown = new ButtonControl();
            buttonDPadLeft = new ButtonControl();
            buttonDPadUp = new ButtonControl();
            buttonDPadRight = new ButtonControl();
            buttonLeftShoulder = new ButtonControl();
            buttonRightShoulder = new ButtonControl();
            buttonLeftStick = new ButtonControl();
            buttonRightStick = new ButtonControl();
            buttonStart = new ButtonControl();
            buttonX = new ButtonControl();
            buttonY = new ButtonControl();

            leftStick = new AxisControl();
            rightStick = new AxisControl();
            leftTrigger = new AnalogSingleAxisControl();
            rightTrigger = new AnalogSingleAxisControl();
        }

        public GamepadDevice(PlayerIndex playerIndex)
            : this()
        {
            this.playerIndex = playerIndex;
        }

        /// <summary>
        /// Returns the actual connection status of the gamepad
        /// </summary>
        public bool IsConnected
        {
            get  { return actState.IsConnected; }
        }

        /// <summary>
        /// Returns true if the InputManager recognized this GamePad as connected during an update since the start of your game
        /// </summary>
        public bool WasEverConnected
        {
            get { return wasEverConnected; }
        }

        public DeviceType DeviceType
        {
            get { return DeviceType.GAMEPAD; }
        }

        public bool HasPlayerIndex
        {
            get { return true; }
        }

        public PlayerIndex PlayerIndex
        {
            get { return playerIndex; }
        }

        public bool HasChatpad
        {
            get
            {
                return GamePad.GetCapabilities(playerIndex).GamePadType == GamePadType.GamePad;
            }
        }

        /// <summary>
        /// Is true if the gamepad was connected since the last update. This is valid only for
        /// one update cycle.
        /// </summary>
        public bool WasConnected
        {
            get { return actState.IsConnected && !lastState.IsConnected; }
        }

        /// <summary>
        /// Is true if the gamepad was disconnected since the last update. This is valid only for
        /// one update cycle.
        /// </summary>
        public bool WasDisconnected
        {
            get { return !actState.IsConnected && lastState.IsConnected; }
        }

        public void Update(GameTime gameTime)
        {
            lastState = actState;
            actState = GamePad.GetState(playerIndex, GamePadDeadZone.None);

            if (actState.IsConnected)
            {
                wasEverConnected = true;

                buttonA.Update(actState.Buttons.A);
                buttonB.Update(actState.Buttons.B);
                buttonBack.Update(actState.Buttons.Back);
                buttonBigButton.Update(actState.Buttons.BigButton);
                buttonDPadDown.Update(actState.DPad.Down);
                buttonDPadLeft.Update(actState.DPad.Left);
                buttonDPadUp.Update(actState.DPad.Up);
                buttonDPadRight.Update(actState.DPad.Right);
                buttonLeftShoulder.Update(actState.Buttons.LeftShoulder);
                buttonRightShoulder.Update(actState.Buttons.RightShoulder);
                buttonLeftStick.Update(actState.Buttons.LeftStick);
                buttonRightStick.Update(actState.Buttons.RightStick);
                buttonStart.Update(actState.Buttons.Start);
                buttonX.Update(actState.Buttons.X);
                buttonY.Update(actState.Buttons.Y);

                leftStick.Update(actState.ThumbSticks.Left);
                rightStick.Update(actState.ThumbSticks.Right);
                leftTrigger.Update(actState.Triggers.Left);
                rightTrigger.Update(actState.Triggers.Right);
            }
        }

        public ButtonControl Button_A
        {
            get { return buttonA; }
        }

        public ButtonControl Button_B
        {
            get { return buttonB; }
        }

        public ButtonControl Button_Back
        {
            get { return buttonBack; }
        }

        public ButtonControl Button_BigButton
        {
            get { return buttonBigButton; }
        }

        public ButtonControl Button_DPadDown
        {
            get { return buttonDPadDown; }
        }

        public ButtonControl Button_DPadLeft
        {
            get { return buttonDPadLeft; }
        }

        public ButtonControl Button_DPadUp
        {
            get { return buttonDPadUp; }
        }

        public ButtonControl Button_DPadRight
        {
            get { return buttonDPadRight; }
        }

        public ButtonControl Button_LeftShoulder
        {
            get { return buttonLeftShoulder; }
        }

        public ButtonControl Button_RightShoulder
        {
            get { return buttonRightShoulder; }
        }

        public ButtonControl Button_LeftStick
        {
            get { return buttonLeftStick; }
        }

        public ButtonControl Button_RightStick
        {
            get { return buttonRightStick; }
        }

        public ButtonControl Button_Start
        {
            get { return buttonStart; }
        }

        public ButtonControl Button_X
        {
            get { return buttonX; }
        }

        public ButtonControl Button_Y
        {
            get { return buttonY; }
        }

        public AxisControl LeftThumbstick
        {
            get { return leftStick; }
        }

        public AxisControl RightThumbstick
        {
            get { return rightStick; }
        }

        public AnalogSingleAxisControl LeftTrigger
        {
            get { return leftTrigger; }
        }

        public AnalogSingleAxisControl RightTrigger
        {
            get { return rightTrigger; }
        }
    }
#endif
}
